package self.wargon.springcloudstu.gateway.config;

import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import reactor.core.publisher.Mono;

@Configuration
public class RequestRateLimiterConfig {

    @Bean
    public KeyResolver ipKeyResolver() {
        //默认的KeyResolver，会尝试从 ServerWebExchange 中获取 Principal，然后调用 Principal.getName() 作为redis的key。如果不配置KeyResolver，并且默认的获取不到Principal，则会报 403 错误。
        return exchange -> Mono.just(
                exchange.getRequest().getRemoteAddress().getAddress().getHostAddress()
        );
    }

}
